﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace Taopi.WebComponents
{

    /// <summary>
    ///RatingLabel 的摘要说明
    /// </summary>
    public class RatingLabel : WebControl
    {
        public RatingLabel()
            : base("span")
        { }

        public decimal Rating
        {
            get { return Convert.ToDecimal((float)ViewState["rating"]); }
            set
            {
                if (value < 0)
                    throw new ArgumentOutOfRangeException("rating不可为负数");
                ViewState["rating"] = (float)value;
            }
        }

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            base.RenderContents(writer);

            int roundedValue = (int)Math.Round(Rating, 0, MidpointRounding.AwayFromZero);

            //TODO:用css repeat，而不是重复标签
            for (int i = 0; i < roundedValue; i++)
            {
                writer.Write("<img alt=\"star\" src=\"{0}\" />", ResolveClientUrl("~/Images/star.jpg"));
            }
        }

        protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute("title", "Rating " + Rating);
        }


    }


}
